package consul

import (
	"fmt"
	"github.com/google/uuid"
	"github.com/hashicorp/consul/api"
	"zy4/common/nacos"
)

func Consul() {
	nac := nacos.Config //获取nacos配置
	//创建consul客户端
	client, err := api.NewClient(&api.Config{
		Address: fmt.Sprintf("%s:%d", nac.Consul.Host, nac.Consul.Port),
	})
	if err != nil {
		return
	}
	id := uuid.New().String() //创建服务id

	//注册服务
	err = client.Agent().ServiceRegister(&api.AgentServiceRegistration{
		ID:      id,               //服务id
		Name:    nac.Consul.Name,  //服务名称
		Tags:    []string{"GRPC"}, //服务标签
		Port:    nac.Grpc.Port,    //服务端口
		Address: nac.Grpc.Host,    //服务地址
		Check: &api.AgentServiceCheck{ //服务检查
			Interval:                       "5s",                                               //检查间隔
			Timeout:                        "5s",                                               //检查超时
			GRPC:                           fmt.Sprintf("%s:%d", nac.Grpc.Host, nac.Grpc.Port), //检查地址
			DeregisterCriticalServiceAfter: "30s",                                              //注销时间
		},
	})
	if err != nil {
		return
	}
}
